Newsgroups: comp.lang.postscript,comp.answers,news.answers Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!spool.mu.edu!howland.reston.ans.net!europa.eng.gtefsd.com!uunet!brunix!doorknob!jgm From: jgm@cs.brown.edu (Jonathan Monsarrat) Subject: PostScript monthly FAQ v2.1 05-21-93 [07-10 of 11] Message-ID: Followup-To: poster Summary: Useful facts about the PostScript graphics programming language Sender: news@cs.brown.edu Supersedes: Reply-To: jgm@cs.brown.edu (PostScript FAQ comments address) Organization: Brown University Department of Computer Science Date: Thu, 26 Aug 1993 15:29:56 GMT Approved: news-answers-request@MIT.Edu Expires: Mon, 27 Sep 1993 00:00:00 GMT Lines: 799 Xref: senator-bedfellow.mit.edu comp.lang.postscript:20192 comp.answers:1747 news.answers:11812 Archive-name: postscript/faq/part7-10 Last-modified: 1993/05/21 Version: 2.1 -- PostScript -- Answers to Questions (the comp.lang.postscript FAQ v2.1) Jon Monsarrat jgm@cs.brown.edu This FAQ is formatted as a digest. Most news readers can skip from one question to the next by pressing control-G. Changes since the last version are marked with a '|' in the table of contents and in the sections in the text-only format of the FAQ. Now that there is Linux and BSD 386 UNIX IBM PC (and clone) users can run any of the X-windows and UNIX programs in the utilities section. See comp.os.linux. Also, there is now GhostScript for the Macintosh. Section 12, Utilities, will be posted in comp.sources.postscript along with the global index from now on. Please help fix the FAQ! All comments should be mailed to jgm@cs.brown.edu. My favorite way to receive a change suggestion is when it is accompanied by a section of the FAQ that is edited and mailed to me verbatim as an example. If you would like to contribute, please read the section ``about the FAQ'' first. Thank you! Books and programs are referred to by name only. See the book sections for book information, and the comp.sources.postscript FAQ for a full list of all PostScript related programs. I have archived a number of the small utilities in wilma.cs.brown.edu:pub/postscript. You can get the comp.sources.postscript FAQ from wilma.cs.brown.edu:pub/comp.sources.postscript. Subject: 7 Programming in PostScript Subject: 7.1 What is PostScript level 2? (See the Section 11, ``About PostScript 2''.) Subject: 7.2 Should I learn level 2 PostScript? Yes, because Level Two will soon become the standard. Application developers using PostScript need to become aware of the new capabilities and how to take advantage of them. There are many good books on PostScript 2. (See Section 5, ``Books''.) Subject: 7.3 Where can I find examples of PostScript code? Many other books on PostScript make example PostScript code available. ``Thinking in PostScript'', by Glenn Reid, is the only book I know of that allows its examples to be freely distributed. (See Section 5, ``Books''.) All the examples in ``the blue book'' are available from the Adobe file server (See Section 5, ``Books''.) See the question ``How can I browse through PostScript programs?'' in the comp.sources.postscript FAQ. Subject: 7.4 How do I get the physical size of a page? The initial clipping path gives you the size of the imagable area. Use ``clippath pathbbox'' to get these coordinates. If you must know the size of the device's imageable area, use the sequence ``gsave initclip clippath pathbbox grestore'', but this will prevent an enclosing application from using the clippath to achieve some special effects (such as multiple pages per page). Subject: 7.5 Why can't I do a pathforall after a charpath ? (See Section 4, ``Fonts'', question ``Why are Adobe fonts hidden?''.) Subject: 7.6 How do I center a string of text around a point? Level 1 PostScript has two operators that can extract information about the metrics of characters: ``stringwidth'' and ``charpath''. The ``stringwidth'' operator returns the advance width of its string operand. This is the distance the current point would be moved by a ``show'' operation on the same string. ``stringwidth'' returns two numbers on the stack, representing the x and y components of the advance width. Usually the y component is zero because most fonts are displayed along a horizontal line, moving the current point only in the x direction. Also note that the ``stringwidth'' usually does not give an exact measure of the area of the page that will be touched by its operand. The letters can either project a little over the boundaries or fall a little within (leaving a touch of whitespace). If all that an application requires is horizontal centering of a long string of text, the result returned by ``stringwidth'' is sufficient. A common technique is x y moveto (string) dup stringwidth pop 2 div neg 0 rmoveto show (This code makes the assumption that the y component of advance width is irrelevant.) The ``charpath'' operator extracts the graphic shapes of its string operand and appends them to the current path in the graphic state. These shapes can then be processed by other PostScript operators. To get the actual size of the area touched by a character a simple approach is gsave newpath 0 0 moveto (X) false charpath flattenpath pathbbox grestore This code places four numbers on the stack, representing the coordinates of the lower left and upper right corners of the bounding box enclosing the character ``X'' rendered with the current point at (0,0). Leaving the flattenpath out will cause it to be less accurate, but it will take up less memory and be faster. There are two things to be careful about when using the code shown above: 1. There are severe limits on the size of the string operand, related to the limit on the number of elements in a graphic path. The PostScript Language Reference Manual recommends taking ``charpath''s one character at a time. 2. If user space is rotated or skewed with respect to device space, the result from ``pathbbox'' may be larger than expected; ``pathbbox'' returns a rectangle oriented along the user space coordinate axes, which fully encloses a (possibly smaller) rectangle oriented along the coordinate axes of device space. If user space is rotated at an integer multiple of 90 degrees these two rectangles will be the same, otherwise the rectangle in user space will be larger. So, to center text vertically one must get the bounding boxes of all the characters in the string to be displayed, find the minimum and maximum y coordinate values, and use half the distance between them to displace the text vertically. This still may not do a very good job, since this provides centering based on extrema, not on the optical center of the string (which is more related to a sort of ``center of mass'' of the text). If an application does this repeatedly, it would be wise to store the bounding boxes in an array indexed by character code, since ``charpath'' is a slow operation. Font metric information is available outside of a PostScript printer in font metrics files, available from the font vendor. A program generating PostScript output can obtain metrics from these files rather than extracting the metrics in the printer. Subject: 7.7 How can I concatenate two strings together? %% string1 string2 append string % Function: Concatenates two strings together. /append { 2 copy length exch length add % find the length of the new. string dup % string1 string2 string string 4 2 roll % string string string1 string2 2 index 0 3 index % string string string1 string2 string 0 string1 putinterval % stuff the first string in. % string string string1 string2 exch length exch putinterval } bind def Subject: 7.8 What do I do when I get stack overflow/underflow? These errors are among the most common in PostScript. When I get a stack overflow, that is usually a sign that a routine is leaving an object on the stack. If this routine gets called 2000 times, it leaves 2000 objects on the stack, which is too many. When I get a stack underflow, that is a sign that either: (A) one of the routines in the program doesn't work, and never has or (B) one of the routines in the program works, but expects to be called with some arguments left on the stack. There is no such thing as a PostScript debugger right now. For now, the best that you can do to debug your program is to put in lots of print statements. Learn to use the PostScript pstack command, and use an online interpreter so you don't have to run to the printer for each debugging cycle. Use an error handler to learn more about what exactly is happening when your program crashes. (see the comp.sources.postscript FAQ for a list of all PostScript related programs.) If your code has never worked yet (i.e. you are still writing it) then I find that it helps to put little comments in the margin about the state of the stack. Like this: Heart pathbbox % lowerx lowery upperx uppery exch 4 -1 roll % lowery uppery upperx lowerx I generally put these comments in originally, and then take them out when the program works. Maybe this is a bad practice, in case I ever want to go back and look at the code to modify it!! Subject: 7.9 How can I print in landscape mode? Landscape (the opposite of portrait) means that the page is turned on its side. You can redefine showpage in terms of the current definition of showpage. Do something like: /oldshowpage /showpage load def 90 rotate llx neg ury neg translate % for the first page /showpage { oldshowpage 90 rotate llx neg ury neg translate } def This won't work if the PostScript file you're editing uses initgraphics or grestoreall. Subject: 8 Computer-specific PostScript This section describes PostScript information specific to a particular type of computer or operating system. Subject: 8.1 Sun Workstations What is NeWS? NeWS is Sun Microsystems PostScript-based window system for the Sun Workstation. NeWS was a project within Sun (started around 1985) to create a window system to supplant SunView (a very successful kernel-based window system). NeWS is a client-server model window system (like X) but among many of NeWS novel features was the use of PostScript as the language to describe the appearance of objects on the screen. NeWS has many features in common with Display PostScript, but NeWS predates Adobe Display PostScript and was neither connected with Adobe Display PostScript nor endorsed by Adobe. NeWS is not an Adobe product, nor is it a Sun/Adobe joint venture. As of October 1992, Sun management signed a deal with Adobe to adopt Display PostScript for the Sun. The future of NeWS is still undecided (but it looks bad). And how does PostScript run on them? PostScript runs on NeWS, although NeWS is not a fully-compliant PostScript interpreter. There were incompatibilities between the NeWS PostScript interpreter and ``official'' PostScript interpreters as defined by Adobe and the Apple LaserWriter family of printers, such that many PostScript files which would print fine on a LaserWriter would not render under NeWS. The most critical incompatibility was the lack of support for Adobe Type 1 fonts, Sun having gone with their own font format known as F3. Subject: 8.2 IBM PC You can find nenscript for OS/2 1.x--2.0 and MSDOS on ftp-os2.nmsu.edu in pub/uploads/nensc113.zip. There are rumors that Word Perfect and Microsoft Word don't produce ``clean'' PostScript that follows the DSC conventions (See Section 9, ``Encapsulated PostScript''). This means that a lot of PostScript utilities like Ghostview and psnup, etc., that require the DSC conventions, will not work on them. Creating a PostScript file from MS Word Install the LaserWriter driver that comes with Windows.In the printer setup, select a PostScript printer. Then click on the setup button to get that pop-up. Then clik the Options button. Then select the print to Encapsulated PostScript File. If you don't specify a file name, Word will prompt you for one when you tell it to print. When printing Microsoft Windows files that have been captured on a PC's LPT port, you mostly need to define two ctrl-d's in a row as well to remove all of them in the document: (\004\004) cvn {} def Subject: 8.3 Apple Macintosh For more details about printing with the Macintosh, read the comp.sys.mac.apps FAQ. How can I convert a PostScript file created with a UNIX program to the Mac? A way that is clumsy, but works, is this: 1. Display the UNIX-based PostScript file on screen 2. Use window dumping facility to get a bitmap file 3. Convert the above bitmap file to TIFF format and then export it to Adobe Illustrator on the Mac. The PostScript section of the FAQ for the Macintosh newsgroup comp.sys.mac.app (maintained by Elliotte Harold) answers the following questions: * How do I make a PostScript file? * How do I print a PostScript file? * Why won't my PostScript file print on my mainframe's printer? Full documentation of this process provided with a utility called macps. * Why are my PostScript files so big? Subject: 9 Encapsulated PostScript Subject: 9.1 What is Encapsulated PostScript? Encapsulated PostScript (EPS) is a standard format for importing and exporting PostScript language files in all environments. It is usually a single page PostScript language program that describes an illustration. The purpose of the EPS file is to be included as an illustration in other PostScript language page descriptions. The EPS file can contain any combination of text, graphics, and images. An EPS file is the same as any other PostScript language page description, with some restrictions. EPS files can optionally contain a bitmapped image preview, so that systems that can't render PostScript directly can at least display a crude representation of what the graphic will look like. There are three preview formats: Mac (PICT), IBM (tiff), and a platform independent preview called EPSI. An EPS file must be a conforming file, that is, it must conform to the Adobe Document Structuring Conventions (DSC). At a minimum, it must include a header comment,%!PS-Adobe-3.0 EPSF-3.0, and a bounding box comment,%%BoundingBox: llx lly urx ury, that describes the bounds of the illustration. (The specification does not require the EPSF version, but many programs will reject a file that does not have it.) The EPS program must not use operators that initialize or permanently change the state of the machine in a manner that cannot be undone by the enclosing application's use of save and restore (eg. the operators starting with ``init'' like initgraphics). As a special case, the EPS program may use the showpage operator. The importing application is responsible for disabling the normal effects of showpage. The EPS program should make no environment-sensitive decisions (the importing application may be trying to attain some special effect, and the EPS program shouldn't screw this up), although it can use some device-dependent tricks to improve appearance such as a snap-to-pixel algorithm. The complete EPS specification is available from Adobe (see the section on Adobe). Read Appendix G (Document Structuring Conventions, V3.0) and Appendix H (Encapsulated PostScript File Format, V3.0) in the new PostScript red book: PostScript Language Reference Manual, Second Edition. An optional component of an EPS file is a ``preview'' image of the file's content. The preview image is a bitmapped representation of the image which may be displayed by programs using the EPS file without having to actually interpret the PostScript code. The recommended form for a preview image is ``Interchange'' format and is described fully in the ``red book'', second edition. Interchange format represents the image as a series of hex strings placed in the EPS file as PostScript comments. The entire file remains an ASCII file. That book contains all of the information that you need to fix your program to correctly output EPS. It is what I use for our software. A variation of EPS embeds the preview image and PostScript text in a binary file which contains a header and the preview image in either a TIFF or MetaFile format. The header defines where in the file each section (EPS, TIFF, or MetaFile) starts and ends. On the Macintosh, the preview is stored as a PICT in the file's resource fork. Subject: 9.2 What are EPSI and EPSF? EPSI is EPS with a device independent bitmap preview. EPSI is an all ASCII (no binary data or headers) version of EPS. EPSI provides for a hexadecimal encoded preview representation of the image that will be displayed or printed. EPSF is a version of EPS with a TIFF preview instead of a bitmap preview. Subject: 9.3 How do I convert PostScript to EPS? Use pstoepsi, or do it by hand. To convert from PostScript to EPS, one must guarantee that the PostScript file meets the above requirements. If the actual program conforms to the programming requirements, then one can simply add the required comments at the top of the file saying that the file is EPS and giving its BoundingBox dimensions. Optional comments include font usage (%%DocumentFonts: or%% DocumentNeededResources: font), EPSI preview comments (%% Begin(End)Preview:) extensions (%%Extensions:) and language level (%%LanguageLevel:). There are some operators that should not be used within an EPS file: banddevice cleardictstack copypage erasepage exitserver framedevice grestoreall initclip initgraphics initmatrix quit renderbands setglobal setpagedevice setshared startjob These also include operators from statusdict and userdict operators like legal, letter, a4, b5, etc. There are some operators that should be carefully used: nulldevice setgstate sethalftone setmatrix setscreen settransfer undefinefont To convert a PostScript file to EPS format, you must edit the file using a text editor or word processor to add lines that will define the file as an EPS-format file. 1. Using your normal method of printing, print the PostScript file to a PostScript printer. You can choose to view it on the screen instead, but keep in mind that all the below distance measurements assume that you are printing on a normal-sized piece of paper. NOTE: If the PostScript image does not get displayed properly, it probably will not work either once you have converted it to EPS format. Correct the PostScript program so that it works before you convert it to EPS format. 2. Use a tool (see below) to find the bounding box, which shows how much space the PostScript image occupies when printed. You specify the dimensions of the bounding box when you convert the PostScript file to EPS format. 3. If you don't have a bounding box tool, you can just use a ruler and draw one on your printout. With two horizontal lines and two vertical lines, draw a box around the image that includes the entire image while minimizing white space. This box represents your bounding box. You may want to leave a small amount of white space around the image as a precautionary measure against minor printing problems, such as paper stretching and paper skewing. 4. Measure distance ``a'' from the lower-left corner of the image to the left edge of the paper. 5. Write the measurement in points. If your ruler does not show points, calculate the total number of points: 1 inch = 72 points, 1 cm = 28.3 points, and 1 pica = 12 points. Designate this measurement as ``measurement a.'' 6. Measure distance ``b'' from the lower-left corner of the image to the bottom edge of the paper. Designate this measurement in points as ``measurement b.'' 7. Measure distance ``c'' from the upper-right corner of the image to the left edge of the paper. Designate this measurement in points as ``measurement c.'' 8. Measure distance ``d' from the upper-right corner of the image to the bottom edge of the paper. Designate this measurement in points as ``measurement d.'' 9. Using any text editor, open the PostScript file for editing. You'll see several lines of text. These lines are the PostScript description of the image. The lines at the top of the file are the header. 10. Add these lines to, or modify existing lines in, the header (the first group of lines in any PostScript file): %!Adobe-2.0 EPSF %%Creator: name %%CreationDate: date %%Title: filename %%BoundingBox: a b c d Note: Make sure that the first line in the file is ``% !Adobe-2.0 EPSF.'' Also, do not separate the header lines with a blank line space. The first blank line that PostScript encounters tells it that the the next line begins the body of the program. For ``name,'' type your name or initials. For ``date,'' type today's date using any format (for example, MM-DD-YY, MM/DD/YY, July 5, 1987, and so on). For ``filename,'' type the name of the PostScript file. After ``BoundingBox: ,'' type the measurements you took in steps 3, 4, 5, and 6, separating each with a space: ``a'' is the measurement from Step 3, ``b'' is the measurement from Step 4, ``c'' is the measurement from Step 5, and ``d'' is the measurement from Step 6. 11. Save the file in text-only format. If you are interested in learning how to further edit your PostScript files, these books are available at most bookstores: Understanding PostScript Programming and the green book. Encapsulated PostScript is discussed in Appendix C of the old red book. The new red book has a lot of information about Encapsulated PostScript. There will be a technical note available from Adobe called ``Guidelines for Specific Operators'' that will talk about why some operators are prohibited and how to use the others. Subject: 9.4 How do I get the bounding box of a PostScript picture? Use bbfig or epsinfo.ps. Or if you would rather construct the bounding box by hand, use Ghostview, which has a continuous readout of the mouse cursor in the default user coordinate system. You simply place the mouse in the corners of the figure and read off the coordinates. Subject: 10 About The Comp.Lang.PostScript FAQ (and Usenet Guide to PostScript) Subject: 10.1 The PostScript FAQ: What is it? The PostScript FAQ is a set of answers to frequently asked questions (FAQs) that have appeared on the Usenet newsgroup comp.lang.postscript. It is broken into many useful sections. The Usenet Guide to PostScript is a larger set of help and answers to PostScript questions, plus a tutorial for new users. It is still in the process of being created. There is one file ``Exactly What Does a Transformation Matrix Do?'', that is definitely not part of the FAQ. Please send more! I need help writing and revising answers for common questions relating to PostScript. Almost all of the information in the documents has been written by kind volunteers. The answers will be published in either or both documents. A very long answer in the Usenet Guide may be summarized, referred to briefly, or not mentioned at all in the FAQ. Subject: 10.2 How to get the FAQ files The FAQ is available by anonymous ftp to wilma.cs.brown.edu:pub/comp.lang.postscript/ You can get it formatted in plain text ASCII, LaTeX, or PostScript. I would be happy to email a copy of the FAQ in any format to you if you do not have FTP. Subject: 10.3 How to write a FAQ answer I greatly appreciate your time and effort to help improve the quality of the FAQ. Thank you for being willing to contribute! * Please check to see if the topic is already in an FAQ. Perhaps you really mean to submit a revision to an existing section. * Start with a clear statement about what problem you are solving. * Write for novice users, in ``tutorial format'', even if the answer is meant for experienced programmers. * Be specific when you make references. * Be complete, and take the time to look over your draft and revise. * Answers should not be too wordy, unless you intend to write a long answer for the Usenet Guide and have a shorter summary or a pointer to the description placed in the FAQ. If you want to write the summary yourself, thanks! * Obviously, I cannot accept copyrighted material without permission. Don't write the FAQ by paraphrasing from a copyrighted book! Subject: 10.4 The FAQ can contain LaTeX and PostScript inserts The FAQ is actually written with LaTeX, so feel free to submit with that text formatting language. There is a PostScript version of the FAQ also, so feel free to send along PostScript pictures to include. Subject: 10.5 Revising the FAQ Suggestions and comments are welcomed. My favorite way of receiving a change suggestion is if you make a copy of the FAQ, edit the copy, and mail me the modification, or a context diff (include the version number). Subject: 10.6 How to submit new information If you know something that you think is worthwhile to be put in a FAQ, definitely send it to me! Don't hold back if your information is very specific. If there's too much information to post I will archive it at an ftp site and place a pointer to it in the FAQ. Subject: 10.7 How to add a program description to the FAQ index If the program is original, please send it to me, or tell me where I can get it. Please put your name and email address at the top of each file. Your program will be doubly useful if you clean up the program so that other people can use it as an example to learn. If the program was written by someone else, please send me just the title, description, and where to get it. I may already have it. For programs the FAQ needs to know: * What is the name of the program? * What does it claim to do, and does it do it well? Is it worth using? * Where is it available? What ftp sites can I get it from? * How much does it cost? Is it free? * What kinds of computers does it run on? * Who is the author and does the author give an email address? * Does it handle PostScript 2? * What packages does it rely on? If the program is a PostScript interpreter, then the FAQ also needs to know: * Does it let you go backwards one page? * Does it display the number of pages in the document? * Does it let you print PostScript to a non-PostScript printer? * What formats can it convert to? Subject: 10.8 How to add a book description to the FAQ For books the FAQ needs to know: * What is the name of the book or document? * What does it claim to do, and does it do it well? Is it worth using? * Can I get it on-line? * Who wrote it? Does the author give an email address? * Who is the publisher, and what is the copyright date? * Does the publisher list an address and phone number or fax number? * What is the ISBN number of the book? * What is the library call number of the book? * How much does the book cost? * Does it cover PostScript 2? * Are coding examples from the book available by email or anonymous ftp? * Do the authors sell the coding examples on a diskette? Subject: 10.9 Questions that need answers 1. Where are ftp sites that have PostScript freeware? 2. What vendors sell fonts for PostScript printers? Where are the free ftp sites for them? 3. What are TrueType fonts? 4. Are there any free encapsulated PostScript converters? 5. What is the charter for comp.lang.postscript? 6. What questions should the FAQ have? 7. What book information is wrong or missing in the FAQ? 8. What program information is wrong or missing in the FAQ? 9. What ftp site have good examples of PostScript code?